﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Providers.Entities;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Serialization;

namespace RantopadService
{
    public class Helper
    {
        /// <summary>
        /// 序列化DataTable
        /// </summary>
        /// <param name="pDt">包含数据的DataTable</param>
        /// <returns>序列化的DataTable</returns>
        public static string SerializeDataTableXml(DataTable pDt)
        {
            // 序列化DataTable
            pDt.TableName = "User";
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sb);
            XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
            serializer.Serialize(writer, pDt);
            writer.Close();

            return sb.ToString();
        }
        /// <summary>
        /// 反序列化DataTable
        /// </summary>
        /// <param name="pXml">序列化的DataTable</param>
        /// <returns>DataTable</returns>
        public static DataTable DeserializeDataTable(string pXml)
        {

            StringReader strReader = new StringReader(pXml);
            XmlReader xmlReader = XmlReader.Create(strReader);
            XmlSerializer serializer = new XmlSerializer(typeof(DataTable));

            DataTable dt = serializer.Deserialize(xmlReader) as DataTable;

            return dt;
        }

        /// <summary> 
        /// 工具方法，Excel处理(解决乱码问题) 
        /// </summary> 
        /// <param name="page">调用页面</param> 
        /// <param name="excel">Excel数据</param> 
        /// <param name="fileName">文件名</param> 
        public static void ExportToExcel(System.Web.UI.Page page, DataTable dt, string FileName)
        {
            HttpResponse resp;
            resp = page.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.ContentType = "application/ms-excel"; 
            //resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            resp.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");
           
           int i = 0;
            try
            {
                
                
                

                //定义表对象与行对象，同时用DataSet对其值进行初始化

                string colHeaders = string.Empty;
                //取得数据表各列标题，各标题之间以\t分割，最后一个列标题后加回车符
                for (i = 0; i < dt.Columns.Count; i++)
                {
                    if (i == (dt.Columns.Count - 1))//最后一列，加\n
                    {
                        colHeaders += dt.Columns[i].Caption.ToString() + "\n";
                    }
                    else
                    {
                        colHeaders += dt.Columns[i].Caption.ToString() + "\t";
                    }

                }
                resp.Write(colHeaders);
                string ls_item = string.Empty;
                DataRow[] myRow = dt.Select();
                
                int cl = dt.Columns.Count;
                foreach (DataRow row in myRow)
                {
                    for (i = 0; i < cl; i++)
                    {
                        if (i == (cl - 1))
                        {
                            ls_item += row[i].ToString() + "\n";
                        }
                        else
                        {
                            ls_item += row[i].ToString() + "\t";
                        }
                    }
                    resp.Output.Write(ls_item);
                    ls_item = string.Empty;
                }
                resp.Output.Flush();
                
                
            }
            catch (Exception e)
            {
                string eror = e.ToString();
                throw;
            }
            finally
            {
                resp.End();
            }
            
        }
    }
}